<?php
if (!defined('APPPATH')) exit('No direct script access allowed');

include_once(APPPATH.'libraries/api/core/MySQL.php');
include_once(APPPATH.'libraries/api/core/Services.php');

class Content extends Services {
    
	public static function contentRecordUpdate($content_id, $column, $column_type, $value) {
		if(parent::checkArguments(array($content_id, $column, $column_type, $value))) {
			$query = "UPDATE `content` SET `$column` = " . MySQL::getColumnValueNotation($column_type, $value) . " WHERE `content_id` = $content_id; ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
    
	public static function contentGet($content_id) {
		if(parent::checkArguments($content_id)) {
			$query = "SELECT `content_id`, `menu_id`, `member_id`, `language_id`, 
                                         `name`, `title`, `content` FROM `content` 
					  WHERE `content_id` = $content_id AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
        
	public static function contentSet($menu_id, $member_id, $language_id, $name, $title, $content) {
		if(parent::checkArguments(array($menu_id, $member_id, $language_id, $name, $title, $content))) {
			$query = "INSERT INTO `content` (`menu_id`, `member_id`, `language_id`, `name`, `title`, `content`, `deleted`) 
					  VALUES ($menu_id, $member_id, $language_id, '$name', '$title', '$content', 0); ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function contentUpdate($content_id, $menu_id, $member_id, $language_id, $name, $title, $content) {
		if(parent::checkArguments(array($content_id, $menu_id, $member_id, $language_id, $name, $title, $content))) {
			$query = "UPDATE `content` SET `menu_id` = $menu_id,
                                           `member_id` = $member_id,
                                           `language_id` = $language_id,
                                           `name` = '$name',
                                           `title` = '$title',
                                           `content` = '$content'
                      WHERE `content_id` = $content_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static function contentDelete($content_id) {
		if(parent::checkArguments($content_id)) {
			$query = "UPDATE `content` SET `deleted` = 1 WHERE `content_id` = $content_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function contentsList() {
		$query = "SELECT `content_id`, `menu_id`, `member_id`, `language_id`, `name`, `title`, `content` FROM `content` WHERE `deleted` != 1; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function contentsLanguageList($language_id) {
		if(parent::checkArguments($language_id)) {
                        $query = "SELECT `content_id`, `menu_id`, `member_id`, `language_id`, `name`, `title`, `content` FROM `content` WHERE `language_id` = $language_id AND `deleted` != 1; ";
                        $result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}        
        
	public static function contentMenuGet($menu_id, $language_id) {
		if(parent::checkArguments(array($menu_id, $language_id))) {
			$query = "SELECT `content_id`, `menu_id`, `member_id`, `language_id`, 
                                         `name`, `title`, `content` FROM `content` 
					  WHERE `menu_id` = $menu_id AND `language_id` = $language_id AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}

}

?>